package com.techsoul.module.pms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.util.bean.BeanUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.core.util.lang.NumberUtils;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.pms.enmus.WarehouseCheckStatusEnum;
import com.techsoul.module.pms.mapper.TWhMaterialCheckLineMapper;
import com.techsoul.module.pms.mapper.TWhMaterialCheckMapper;
import com.techsoul.module.pms.mapper.TbWmsLocationMapper;
import com.techsoul.module.pms.mapper.TbWmsMaterialInLocationMapper;
import com.techsoul.module.pms.pojo.entity.TWhMaterialCheck;
import com.techsoul.module.pms.pojo.entity.TWhMaterialCheckLine;
import com.techsoul.module.pms.pojo.entity.TbWmsLocation;
import com.techsoul.module.pms.pojo.entity.TbWmsMaterialInLocation;
import com.techsoul.module.pms.pojo.param.TWhMaterialCheckLineSaveParam;
import com.techsoul.module.pms.pojo.param.TWhMaterialCheckLineSearchParam;
import com.techsoul.module.pms.pojo.param.TbWmsMaterialInLocationSearchParam;
import com.techsoul.module.pms.pojo.vo.TWhMaterialCheckLineVo;
import com.techsoul.module.pms.service.IBmsAllGeneralSeqTabService;
import com.techsoul.module.pms.service.ITWhMaterialCheckLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 盘点明细 Service实现
 *
 * @author liuen
 * @date 2022-02-17
 */
@Service
@Transactional(readOnly = true)
public class TWhMaterialCheckLineServiceImpl implements ITWhMaterialCheckLineService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TWhMaterialCheckLineMapper mapper;
    @Autowired
    private TWhMaterialCheckMapper tWhMaterialCheckMapper;
    @Autowired
    private TbWmsLocationMapper tbWmsLocationMapper;
    @Autowired
    private IBmsAllGeneralSeqTabService allGeneralSeqTabService;
    @Autowired
    private TbWmsMaterialInLocationMapper tbWmsMaterialInLocationMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insert(TWhMaterialCheckLineSaveParam params) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        TWhMaterialCheckLine entity = new TWhMaterialCheckLine();
        BeanUtils.copyProperties(params, entity);
        entity.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
        this.mapper.insert(entity);
        return entity.getId();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {
        return this.mapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            return this.mapper.deleteBatchIds(idList);
        } else {
            return 0;
        }
    }

    /**
     * 修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer update(TWhMaterialCheckLineSaveParam params) {
        TWhMaterialCheckLine entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entityFromDb);
        entityFromDb.preUpdate(sysUser.getId());
        return this.mapper.updateById(entityFromDb);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public TWhMaterialCheckLineVo findById(String id) {
        return this.mapper.findById(id);
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<TWhMaterialCheckLineVo> findAll(TWhMaterialCheckLineSearchParam params) {
        return this.mapper.findAll(params);
    }

    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<TWhMaterialCheckLineVo> findAll(TWhMaterialCheckLineSearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAll(new Page<>(current, pageSize), params);
    }
    @Transactional(readOnly = false)
    @Override
    public String startCheck(String locationNo){
        String rt = "success";
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        List<TbWmsMaterialInLocation> materials = tbWmsMaterialInLocationMapper.findByLocationNo(locationNo);
        for(int i=0; i <materials.size();i++){
            TbWmsMaterialInLocation tbWmsMaterialInLocation = (TbWmsMaterialInLocation)materials.get(i);
            tbWmsMaterialInLocation.setCheckNo(new Long(0));
            tbWmsMaterialInLocation.preUpdate(sysUser.getId());
            this.tbWmsMaterialInLocationMapper.updateById(tbWmsMaterialInLocation);
        }
        return rt;
    }
    /**
     * 盘点
     * @param rfidList
     *
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String processCheck(List<String> rfidList,List<String> counts) {
        String rt = "success";
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();

        if (ListUtils.isNotEmpty(rfidList)) {
            //  List<String> tbWmsMaterialInLocationIds = new ArrayList<String>();
            TWhMaterialCheck tWhMaterialCheck = new TWhMaterialCheck();
            for (int i = 0; i < rfidList.size(); i++) {
                String materialInLocId = (String) rfidList.get(i);
                String ct = (String)counts.get(i);
                Long lct = Long.valueOf(ct);
                if(i==0){
                    List<TbWmsMaterialInLocation> tbWmsMaterialInLocations =  tbWmsMaterialInLocationMapper.findByrfid(materialInLocId);
                    if(tbWmsMaterialInLocations!=null) {
                        TbWmsMaterialInLocation tbWmsMaterialInLocation = (TbWmsMaterialInLocation) tbWmsMaterialInLocations.get(0);

                        TbWmsLocation tbWmsLocation = tbWmsLocationMapper.findByLocationNo(tbWmsMaterialInLocation.getLocationNo());
                        String checkNo = allGeneralSeqTabService.generateNo("check_material");
                        tbWmsMaterialInLocation.setCheckNo(Long.valueOf(checkNo));
                        tbWmsMaterialInLocation.preUpdate(sysUser.getId());
                        this.tbWmsMaterialInLocationMapper.updateById(tbWmsMaterialInLocation);

                        TbWmsMaterialInLocationSearchParam param = new TbWmsMaterialInLocationSearchParam();
                        param.setLocationNo(tbWmsMaterialInLocation.getLocationNo());
                        //  tbWmsMaterialInLocationIds =tbWmsMaterialInLocationMapper.findAllIds(param);
                        tWhMaterialCheck.setStatus(WarehouseCheckStatusEnum.created.getStatus());
                        tWhMaterialCheck.setCheckNo(checkNo);
                        tWhMaterialCheck.setVersion(new Long(0));
                        tWhMaterialCheck.setWarehouseCode(tbWmsLocation.getWarehouseCode());
                        tWhMaterialCheck.setCustomerSearch(tbWmsMaterialInLocation.getCustomerNo());
                        tWhMaterialCheck.setSiteCode(sysUser.getOrgId());
                        tWhMaterialCheck.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
                        this.tWhMaterialCheckMapper.insert(tWhMaterialCheck);
                        TWhMaterialCheckLine tWhMaterialCheckLine = new TWhMaterialCheckLine();
                        tWhMaterialCheckLine.setBatchNo(tbWmsMaterialInLocation.getBatchNo());
                        tWhMaterialCheckLine.setCheckBy(sysUser.getUserName());
                        tWhMaterialCheckLine.setCheckNo(tWhMaterialCheck.getCheckNo());
                        tWhMaterialCheckLine.setCheckTime(new Date());
                        tWhMaterialCheckLine.setCustomerNo(tbWmsMaterialInLocation.getCustomerNo());
                        tWhMaterialCheckLine.setLineNo(Long.valueOf(i));
                        tWhMaterialCheckLine.setVersion(new Long(0));
                        tWhMaterialCheckLine.setWarehouseCode(sysUser.getCustomerId());
                        tWhMaterialCheckLine.setWarehouseCode(tWhMaterialCheck.getWarehouseCode());
                        tWhMaterialCheckLine.setSiteCode(sysUser.getOrgId());
                        tWhMaterialCheckLine.setLocationNo(tbWmsMaterialInLocation.getLocationNo());
                        tWhMaterialCheckLine.setMaterialNo(tbWmsMaterialInLocation.getMaterialNo());
                        tWhMaterialCheckLine.setPhysicalCountQuantity(BigDecimal.valueOf(lct));
                        tWhMaterialCheckLine.setPhysicalInventory(NumberUtils.toScaledBigDecimal(tbWmsMaterialInLocation.getQuantity()));
                        if (ct.equals(tbWmsMaterialInLocation.getQuantity())) {
                            tWhMaterialCheckLine.setStatus(WarehouseCheckStatusEnum.correct.getStatus());
                        } else if (lct.doubleValue() > tbWmsMaterialInLocation.getQuantity().doubleValue()) {
                            tWhMaterialCheckLine.setStatus(WarehouseCheckStatusEnum.qtyMore.getStatus());
                        } else {
                            tWhMaterialCheckLine.setStatus(WarehouseCheckStatusEnum.qtyLess.getStatus());
                        }

                        tWhMaterialCheckLine.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
                        this.mapper.insert(tWhMaterialCheckLine);
                    }
                }else{
                    List<TbWmsMaterialInLocation> tbWmsMaterialInLocations =  tbWmsMaterialInLocationMapper.findByrfid(materialInLocId);
                    if(tbWmsMaterialInLocations!=null) {
                        TbWmsMaterialInLocation tbWmsMaterialInLocation = (TbWmsMaterialInLocation) tbWmsMaterialInLocations.get(0);
                        tbWmsMaterialInLocation.setCheckNo(Long.valueOf(tWhMaterialCheck.getCheckNo()));
                        tbWmsMaterialInLocation.preUpdate(sysUser.getId());
                        this.tbWmsMaterialInLocationMapper.updateById(tbWmsMaterialInLocation);
                        TWhMaterialCheckLine tWhMaterialCheckLine = new TWhMaterialCheckLine();
                        tWhMaterialCheckLine.setBatchNo(tbWmsMaterialInLocation.getBatchNo());
                        tWhMaterialCheckLine.setCheckBy(sysUser.getUserName());
                        tWhMaterialCheckLine.setCheckNo(tWhMaterialCheck.getCheckNo());
                        tWhMaterialCheckLine.setCheckTime(new Date());
                        tWhMaterialCheckLine.setCustomerNo(tbWmsMaterialInLocation.getCustomerNo());
                        tWhMaterialCheckLine.setLineNo(Long.valueOf(i));
                        tWhMaterialCheckLine.setSiteCode(sysUser.getOrgId());
                        tWhMaterialCheckLine.setWarehouseCode(tWhMaterialCheck.getWarehouseCode());
                        tWhMaterialCheckLine.setVersion(new Long(0));
                        tWhMaterialCheckLine.setLocationNo(tbWmsMaterialInLocation.getLocationNo());
                        tWhMaterialCheckLine.setMaterialNo(tbWmsMaterialInLocation.getMaterialNo());
                        tWhMaterialCheckLine.setPhysicalCountQuantity(BigDecimal.valueOf(lct));
                        tWhMaterialCheckLine.setPhysicalInventory(NumberUtils.toScaledBigDecimal(tbWmsMaterialInLocation.getQuantity()));
                        if (ct.equals(tbWmsMaterialInLocation.getQuantity())) {
                            tWhMaterialCheckLine.setStatus(WarehouseCheckStatusEnum.correct.getStatus());
                        } else if (lct.doubleValue() > tbWmsMaterialInLocation.getQuantity().doubleValue()) {
                            tWhMaterialCheckLine.setStatus(WarehouseCheckStatusEnum.qtyMore.getStatus());
                        } else {
                            tWhMaterialCheckLine.setStatus(WarehouseCheckStatusEnum.qtyLess.getStatus());
                        }
                        tWhMaterialCheckLine.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
                        this.mapper.insert(tWhMaterialCheckLine);
                    }
                }
            /*    for(int j = 0 ;j <tbWmsMaterialInLocationIds.size();j++){
                    String materialid = (String)tbWmsMaterialInLocationIds.get(j);
                    if(!rfidList.contains(materialid)){
                        TbWmsMaterialInLocationVo tbWmsMaterialInLocation =  tbWmsMaterialInLocationMapper.findById(materialid);
                        TWhMaterialCheckLine tWhMaterialCheckLine = new TWhMaterialCheckLine();
                        tWhMaterialCheckLine.setBatchNo(tbWmsMaterialInLocation.getBatchNo());
                        tWhMaterialCheckLine.setCheckBy(sysUser.getUserName());
                        tWhMaterialCheckLine.setCheckNo(tWhMaterialCheck.getCheckNo());
                        tWhMaterialCheckLine.setCheckTime(new Date());
                        tWhMaterialCheckLine.setSiteCode(tWhMaterialCheck.getSiteCode());
                        tWhMaterialCheckLine.setCustomerNo(tbWmsMaterialInLocation.getCustomerNo());
                        tWhMaterialCheckLine.setLineNo(Long.valueOf(i));
                        tWhMaterialCheckLine.setVersion(new Long(0));
                        tWhMaterialCheckLine.setWarehouseCode(tWhMaterialCheck.getWarehouseCode());
                        tWhMaterialCheckLine.setLocationNo(tbWmsMaterialInLocation.getLocationNo());
                        tWhMaterialCheckLine.setMaterialNo(tbWmsMaterialInLocation.getMaterialNo());
                        tWhMaterialCheckLine.setPhysicalCountQuantity(new BigDecimal(0.0));
                        tWhMaterialCheckLine.setPhysicalInventory(tbWmsMaterialInLocation.getQuantity());
                        tWhMaterialCheckLine.setStatus(WarehouseCheckStatusEnum.missing.getStatus());
                        tWhMaterialCheckLine.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
                        this.mapper.insert(tWhMaterialCheckLine);
                    }
                }*/

            }
        }
        return rt;
    }
}
